from pydantic import BaseModel, Field
from typing import Optional, Dict, Any, List
from datetime import datetime
from decimal import Decimal


class FlightLogBase(BaseModel):
    """
    飞行日志基础模型
    """
    # 位置信息
    latitude: float = Field(..., description="纬度")
    longitude: float = Field(..., description="经度")
    altitude: float = Field(..., description="高度（米）")
    
    # 飞行状态
    speed: float = Field(..., description="速度（米/秒）")
    heading: float = Field(..., description="航向角（度）")
    roll: Optional[float] = Field(None, description="横滚角（度）")
    pitch: Optional[float] = Field(None, description="俯仰角（度）")
    yaw: Optional[float] = Field(None, description="偏航角（度）")
    
    # 电机信息
    motor_speed_1: Optional[float] = Field(None, description="电机1转速")
    motor_speed_2: Optional[float] = Field(None, description="电机2转速")
    motor_speed_3: Optional[float] = Field(None, description="电机3转速")
    motor_speed_4: Optional[float] = Field(None, description="电机4转速")
    
    # 电池信息
    battery_voltage: float = Field(..., description="电池电压（V）")
    battery_current: Optional[float] = Field(None, description="电池电流（A）")
    battery_percentage: Optional[int] = Field(None, description="电池电量百分比")
    
    # 环境信息
    temperature: Optional[float] = Field(None, description="温度（摄氏度）")
    humidity: Optional[float] = Field(None, description="湿度（百分比）")
    wind_speed: Optional[float] = Field(None, description="风速（米/秒）")
    wind_direction: Optional[float] = Field(None, description="风向（度）")
    
    # 系统状态
    cpu_temperature: Optional[float] = Field(None, description="CPU温度")
    gpu_temperature: Optional[float] = Field(None, description="GPU温度")
    ram_usage: Optional[float] = Field(None, description="内存使用率")
    
    # 任务执行状态
    task_progress: Optional[int] = Field(None, description="任务进度百分比")
    current_waypoint_index: Optional[int] = Field(None, description="当前航点索引")
    
    # 其他信息
    flight_mode: Optional[str] = Field(None, description="飞行模式")
    gps_satellites: Optional[int] = Field(None, description="GPS卫星数量")
    gps_accuracy: Optional[float] = Field(None, description="GPS精度")
    error_code: Optional[str] = Field(None, description="错误代码")
    error_message: Optional[str] = Field(None, description="错误信息")
    
    # 扩展数据
    extended_data: Optional[Dict[str, Any]] = Field(None, description="扩展数据")


class FlightLogCreate(FlightLogBase):
    """
    创建飞行日志请求模型
    """
    drone_id: int = Field(..., description="无人机ID")
    task_id: Optional[int] = Field(None, description="任务ID")


class FlightLogUpdate(BaseModel):
    """
    更新飞行日志请求模型
    """
    # 可选更新字段
    task_id: Optional[int] = Field(None, description="任务ID")
    error_code: Optional[str] = Field(None, description="错误代码")
    error_message: Optional[str] = Field(None, description="错误信息")
    extended_data: Optional[Dict[str, Any]] = Field(None, description="扩展数据")


class FlightLog(FlightLogBase):
    """
    飞行日志响应模型
    """
    id: int
    drone_id: int
    task_id: Optional[int]
    record_time: datetime
    created_at: datetime
    updated_at: datetime
    
    # 关联信息（可选）
    drone_name: Optional[str] = None
    task_name: Optional[str] = None
    
    class Config:
        from_attributes = True


class FlightLogList(BaseModel):
    """
    飞行日志列表响应模型
    """
    code: int = Field(200, description="状态码")
    message: str = Field("获取成功", description="消息")
    data: Dict[str, Any] = Field(..., description="数据")
    
    class Config:
        json_schema_extra = {
            "example": {
                "code": 200,
                "message": "获取成功",
                "data": {
                    "items": [
                        {
                            "id": 1,
                            "drone_id": 1,
                            "task_id": 1,
                            "latitude": 39.9042,
                            "longitude": 116.4074,
                            "altitude": 100.0,
                            "speed": 5.0,
                            "heading": 0.0,
                            "battery_voltage": 12.6,
                            "record_time": "2023-10-01T12:00:00",
                            "drone_name": "Drone001",
                            "task_name": "巡逻任务"
                        }
                    ],
                    "total": 100,
                    "page": 1,
                    "page_size": 10,
                    "pages": 10
                }
            }
        }


class FlightTrajectoryPoint(BaseModel):
    """
    飞行轨迹点模型
    """
    timestamp: datetime
    latitude: float
    longitude: float
    altitude: float
    speed: float


class FlightTrajectory(BaseModel):
    """
    飞行轨迹响应模型
    """
    code: int = 200
    message: str = "获取成功"
    data: Dict[str, Any] = Field(..., description="轨迹数据")
    
    class Config:
        json_schema_extra = {
            "example": {
                "code": 200,
                "message": "获取成功",
                "data": {
                    "drone_id": 1,
                    "drone_name": "Drone001",
                    "task_id": 1,
                    "task_name": "巡逻任务",
                    "start_time": "2023-10-01T12:00:00",
                    "end_time": "2023-10-01T13:00:00",
                    "points": [
                        {
                            "timestamp": "2023-10-01T12:00:00",
                            "latitude": 39.9042,
                            "longitude": 116.4074,
                            "altitude": 100.0,
                            "speed": 5.0
                        }
                    ]
                }
            }
        }